import {
	requestUrl
} from "./config.js"
export function request(url, method, data) {
	return new Promise((reslove, reject) => {
		uni.request({
			url: requestUrl + url,
			method,
			dataType: "json",
			header: {
				"content-type": "application/json",
				"Authorization":'Bearer '+uni.getStorageSync("TOKEN")
			},
			timeout:30000,
			data,
			success: (res) => {
				uni.hideLoading()
				if (res.data.code == 401) {
					reject(res.data)
					uni.removeStorageSync("TOKEN")
					uni.reLaunch({
						url:"/pages/login/login",
						success() {
							plus.navigator.closeSplashscreen();
						}
					})
				} else if (res.data.code == 200) {
					reslove(res.data)
				} else {
					reject(res.data)
				}
			},
			fail: (err) => {
				console.log(err)
				uni.showModal({
					content:"故障排查指南:\n1.网络超时,检查网络重新尝试!\n2.服务器停止工作,反馈运营人员!"
				})
				uni.hideLoading()
				reject(err)
			},
			complete: function() {
				uni.hideLoading()
			}

		})
	})
}
export function requestOssTxt(url){
	return new Promise((reslove,reject)=>{
		uni.request({
			url,
			method:'GET',
			dataType: "string",
			timeout:30000,
			success: (res) => {
				uni.hideLoading()
				if(res.statusCode==200){
					reslove(res.data)
				}else{
					reject(res.data)
				}
			},
			fail: (err) => {
				uni.showModal({
					content:"网络超时，请检查网络重新尝试！\n点击取消仍可去离线阅读！"
				})
				uni.hideLoading()
				reject(err)
			},
			complete: function() {
				uni.hideLoading()
			}
		
		})
	})
}